package com.jackrain.nea.fc.pos.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.pos.api.ViewDLRetailPriceToF4QueryCmd;
import com.jackrain.nea.fc.pos.mapper.ViewDLRetailPriceToF4Mapper;
import com.jackrain.nea.fc.utils.Constants;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 提交 触发零售价格调整
 *
 * @author wuwenxue
 * @date 2018/05/22
 **/
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "pos")
public class ViewDLRetailPriceToF4QueryCmdImpl extends CommandAdapter implements ViewDLRetailPriceToF4QueryCmd {

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        ValueHolder vh = new ValueHolder();
        ViewDLRetailPriceToF4Mapper viewDLRetailPriceToF4Mapper = ApplicationContextHandle.getBean(
                ViewDLRetailPriceToF4Mapper.class);
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(
                map.get("param"), "yyyy-MM-dd HH:mm:ss"), Feature.OrderedField);
        log.info("ViewDLRetailPriceToF4QueryCmdImpl-->param-->{}",param);

        if (null == param || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }

        JSONArray ids = param.getJSONArray("IDS");
        String action = param.getString("action");
        JSONArray jsonArray = new JSONArray();

        if ("submit".equals(action)) {
            JSONArray notProerror = new JSONArray();
            //验收
            List<Long> proType = new ArrayList<>();
            for (int i = 0; i < ids.size(); i++) {
                String type = viewDLRetailPriceToF4Mapper.selRetailPriceDate(ids.getLong(i));
                //按款调价
                if ("PRO".equals(type)) {
                    proType.add(ids.getLong(i));
                } else {
//                    log.debug("非按款调价方式！type->" + type + "  id->" + ids.getLong(i));
                    notProerror.add("非按款调价方式！type->" + type + "  id->" + ids.getLong(i));
                }
            }

            if (null != proType && proType.size() > 0) {
                Object[] idsObj = proType.toArray();
                List<HashMap> list = viewDLRetailPriceToF4Mapper.selRetailPriceItemData(idsObj);
                for (HashMap hashMap : list) {
                    JSONObject js = new JSONObject(hashMap);
                    JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(js,
                            "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                    jsonArray.add(param1);
                }
            }

            if (null != notProerror && notProerror.size() > 0) {
                vh.put("message", notProerror);
            }
        }


        vh.put("data", jsonArray);
        return vh;
    }


}
